package com.cy.pj.modules.controller;

import com.cy.pj.common.vo.JsonResult;
import com.cy.pj.modules.entity.Category;
import com.cy.pj.modules.service.ICategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 类目Controller
 *
 * @author kiven
 * @date 2023-02-09
 */
@Controller
@RequestMapping("/category")
public class CategoryController
{
    private String prefix = "modules/category";

    @Autowired
    private ICategoryService categoryService;

    @RequiresPermissions("category:view")
    @GetMapping()
    public String category()
    {
        return prefix + "/category_list";
    }

    /**
     * 查询类目树列表
     */
    @RequiresPermissions("category:list")
    @GetMapping("/list")
    @ResponseBody
    public JsonResult list(Category category)
    {
        return new JsonResult(categoryService.selectCategoryList(category));
    }

    ///**
    // * 导出类目列表
    // */
    //@RequiresPermissions("category:export")
    //@PostMapping("/export")
    //@ResponseBody
    //public AjaxResult export(Category category)
    //{
    //    List<Category> list = categoryService.selectCategoryList(category);
    //    ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
    //    return util.exportExcel(list, "category");
    //}

    /**
     * 新增类目
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (id != null )
        {
            mmap.put("category", categoryService.selectCategoryById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存类目
     */
    @RequiresPermissions("category:add")
    @PostMapping("/add")
    @ResponseBody
    public JsonResult addSave(Category category)
    {
        return new JsonResult(categoryService.insertCategory(category));
    }

    /**
     * 修改类目
     */
    @GetMapping("/edit")
    public String edit( Long id, ModelMap mmap)
    {
        Category category = categoryService.selectCategoryById(id);
        mmap.put("category", category);
        return prefix + "/category_edit";
    }

    /**
     * 修改保存类目
     */
    @RequiresPermissions("category:update")
    @PostMapping("/edit")
    @ResponseBody
    public JsonResult editSave(Category category)
    {
        return new JsonResult(categoryService.updateCategory(category));
    }

    /**
     * 删除
     */
    @RequiresPermissions("category:delete")
    @PostMapping("/remove")
    @ResponseBody
    public JsonResult remove(Long id)
    {
        return new JsonResult(categoryService.deleteCategoryById(id));
    }

    /**
     * 选择类目树
     */
    @GetMapping(value = { "/selectCategoryTree/{id}", "/selectCategoryTree/" })
    public String selectCategoryTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (id!=null)
        {
            mmap.put("category", categoryService.selectCategoryById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载类目树列表
     */
    @GetMapping("/doFindZtreeMenuNodes")
    @ResponseBody
    public JsonResult treeData()
    {

        return  new JsonResult(categoryService.selectCategoryTree());
    }
}
